<?php

namespace app\api\controller\sign;

use app\api\model\SignIn;
use app\common\controller\Api;

use think\Db;

/**
 * 首页接口
 */
class Sign extends Api
{
//    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new SignIn();
    }

    /**
     * 签到列表
     *
     */
    public function index()
    {

        $userinfo = $this->auth->getUserinfo();
        $sort = 'id';
        $order = 'DESC';
        $offset = 1;
        $limit = 10;
        $profile_id = Db::name('classes_teacher')->where('user_id', $userinfo['user_id'])->value('profile_id');
        $where['profile_id'] = $profile_id;
        $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        $rows = $list->toArray()['data'];
        foreach ($rows as &$row) {
            $row['createtime_formt'] = date("Y-m-d", $row['createtime']);
        }

        $result = array("total" => $list->total(), "rows" => $rows);

        return json($result);
    }

    /**
     * Notes:创建签到
     * DateTime: 2024/7/16 17:32
     */
    public function add(){
        $title=$this->request->post('title/s','');
        $end_time=$this->request->post('end_time/s','');
        $stype=$this->request->post('stype/d',0);
        $class_id=$this->request->post('class_id/d','');
        if(empty($title)||empty($end_time)||empty($class_id)){
            return $this->error('缺少重要参数');
        }
        $userinfo = $this->auth->getUserinfo();
        $profile_id = Db::name('classes_teacher')->where('user_id', $userinfo['user_id'])->value('profile_id');
        $data['title']=$title;
        $data['type']=$stype;
        $data['end_time']=strtotime($end_time);
        $data['class_id']=$class_id;
        $data['profile_id']=$profile_id;
        $this->model->save($data);
        return $this->success('添加成功');
    }



}
